const axios = require('axios');
const cheerio = require('cheerio');
const { saveToJson } = require('./saveData');

class Crawler {
    constructor() {
        this.globalIndex = 0;
    }

    async crawlPage(url, selector) {
        try {
            const response = await axios.get(url, {
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
                },
                responseType: 'arraybuffer'
            });

            const html = new TextDecoder('gbk').decode(response.data);
            const $ = cheerio.load(html, { decodeEntities: false });
            const pageData = [];

            $(selector).each((_, element) => {
                const $element = $(element);
                const item = {
                    url: url,
                    index: this.globalIndex++,
                    title: $element.text().trim(),
                    link: 'https://speedm.qq.com' + $element.attr('href')
                };
                pageData.push(item);
            });

            return pageData;

        } catch (error) {
            console.error(`爬取页面 ${url} 时发生错误：${error.message}`);
            return [];
        }
    }

    async crawl(urls, selector, outputFile) {
        try {
            const allData = [];

            for (const url of urls) {
                const pageData = await this.crawlPage(url, selector);
                allData.push(...pageData);
                await new Promise(resolve => setTimeout(resolve, 1000));
            }

            const saved = saveToJson(allData, outputFile);
            if (saved) {
                return {
                    success: true,
                    message: `成功爬取 ${allData.length} 条数据`,
                    data: allData
                };
            }

        } catch (error) {
            console.error('爬取过程中发生错误：', error.message);
            return {
                success: false,
                message: error.message
            };
        }
    }

    async crawlSingle(url, selectors) {
        try {
            const response = await axios.get(url, {
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
                },
                responseType: 'arraybuffer'
            });

            const html = new TextDecoder('gbk').decode(response.data);
            const $ = cheerio.load(html, { decodeEntities: false });
            const result = {
                url: url,
                timestamp: new Date().toISOString(),
                data: {}
            };

            // 处理多个选择器
            for (const key in selectors) {
                const selector = selectors[key];
                result.data[key] = [];

                $(selector).each((_, element) => {
                    const $element = $(element);
                    const item = {
                        text: $element.text().trim(),
                        html: $element.html(),
                        href: $element.attr('href') ? 
                              ($element.attr('href').startsWith('http') ? 
                               $element.attr('href') : 
                               'https://speedm.qq.com' + $element.attr('href')) : null,
                        attributes: {}
                    };

                    // 获取元素的所有属性
                    element.attribs && Object.keys(element.attribs).forEach(attr => {
                        item.attributes[attr] = element.attribs[attr];
                    });

                    result.data[key].push(item);
                });

            }

            return {
                success: true,
                message: `成功爬取所有数据`,
                result
            };

        } catch (error) {
            console.error(`爬取页面 ${url} 时发生错误：${error.message}`);
            return {
                success: false,
                message: error.message
            };
        }
    }
}

module.exports = Crawler;